package com.ruisoft.business.attendance.service;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.poi.util.StringUtil;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Service;

import com.ruisoft.base.bean.QueryData;
import com.ruisoft.base.service.BaseService;
import com.ruisoft.common.SysCache;
import com.ruisoft.core.dml.entity.DMLEntity;
import com.ruisoft.core.dml.entity.QueryEntity;

@Service
public class AttendService extends BaseService  {

    private static final Logger LOG = Logger.getLogger(AttendService.class);
    
	public JSONObject saveMachinfo(JSONObject jsonData) throws Exception {
		baseDao.add(jsonData, "attendance.add.machineinfo.add");
		return jsonData;
	}
    
	
	public JSONObject loadAttendance(JSONObject reqData) throws Exception{
		JSONArray items = reqData.getJSONArray("Rows");
        JSONObject[] item = new JSONObject[items.length()];
        for (int i = 0; i < items.length(); i++) {
            item[i] = items.getJSONObject(i);
            baseDao.add(item[i], "attendance.add.loadAttendance.add");
        }
		
		return reqData;
	}
	
	public List<JSONObject> queryAttend(QueryData q) throws Exception{
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT machCode, machSn,machIp,machName,attendinfo.verify_method,attendtime, ");
		sql.append(" attendinfo.userid AS userid,name AS username , ssn   ");
		sql.append(" FROM attendinfo  ");
		sql.append(" LEFT JOIN employeeinfo ON attendinfo.userid = employeeinfo.userid  ");
		JSONObject json = q.paramsData;
		StringBuffer where = new StringBuffer();
		boolean flg = false;
		if(json.has("userid")&&!StringUtils.isEmpty((String)json.get("userid"))){
			where.append(" attendinfo.userid = '").append((String)json.get("userid")).append("' ");
			flg = true;
		}
		if(json.has("ssn")&&!StringUtils.isEmpty((String)json.get("ssn"))){
			if(flg){
				where.append(" AND ");
			}
			where.append(" ssn like '%").append((String)json.get("ssn")).append("%' ");
			flg = true;
		}
		if(json.has("name")&&!StringUtils.isEmpty((String)json.get("name"))){
			if(flg){
				where.append(" AND ");
			}
			where.append(" name like '%").append((String)json.get("name")).append("%' ");
			flg = true;
		}
		if(json.has("machname")&&!StringUtils.isEmpty((String)json.get("machname"))){
			if(flg){
				where.append(" AND ");
			}
			where.append(" machname like '%").append((String)json.get("machname")).append("%' ");
			flg = true;
		}
		if(json.has("timestart")&&!StringUtils.isEmpty((String)json.get("timestart"))){
			if(flg){
				where.append(" AND ");
			}
			where.append(" attendtime >= '").append((String)json.get("timestart")).append("' ");
			flg = true;
		}
		if(json.has("timeend")&&!StringUtils.isEmpty((String)json.get("timeend"))){
			if(flg){
				where.append(" AND ");
			}
			where.append(" attendtime <= '").append((String)json.get("timeend")).append("' ");
			flg = true;
		}
		if(where.toString().length()>0){
			sql.append(" where ").append(where.toString());
		}
		 LOG.debug("查询SQL".concat(sql.toString()));
		List<JSONObject> result = baseDao.queryForPage(sql.toString(),q.curPage,q.pageSize);
		return result;
	}
	
	
	public int delAttendTemp(){
		int result = 0;
		String sql = "DELETE FROM attendinfotemp";
		result = baseDao.update(sql);
		return result;
	}
	
	public void updateAttend(){
		StringBuffer sql = new StringBuffer();
		sql.append(" INSERT INTO attendinfo (machCode, machSn,machName,machIp, userid, verify_method, attend_status, attendtime, create_uid, create_date) ");
		sql.append(" SELECT machCode, machSn,machName,machIp, userid, verify_method, attend_status, attendtime, create_uid, create_date FROM attendinfotemp  ");
		sql.append(" WHERE  NOT EXISTS  ");
		sql.append(" (SELECT 1 FROM attendinfo WHERE attendinfotemp.machSn = attendinfo.machSn  ");
		sql.append("  AND attendinfotemp.userid =attendinfo.userid ");
		sql.append("  AND attendinfotemp.attendtime = attendinfo.attendtime)  ");
		baseDao.execute(sql.toString());
	}
	
    public JSONObject searchMach(String snn) throws Exception {
        List<JSONObject> machList = baseDao.queryId(snn, "attendance.select.searchMach");
        JSONObject result = machList.get(0);
        return result;
    }
    
    
    public JSONObject updateMachinfo(JSONObject jsonData) throws Exception {
        baseDao.update(jsonData, "attendance.update.updateMachinfo");
        return jsonData;
    }
	
    public int deleteMachinfo(String snn) throws Exception{
        int status = 0;
        status = baseDao.deleteCode(snn,"attendance.delete.deleteMachinfo");
        return status;
    }
}
